<?php

class Service_Api_Promo extends Contract_Api_Promo
{
    public function promo_list($params): array
    {
        $date       = strtotime(date('Y-m-d'));
        $page_link = str_replace('/','\\\\\\\\/',$params['path']);
        $promo_list = DB::select()->from('promo')
            ->where('is_open', '=', 1)
            ->and_where('start_date', '<=', $date)
            ->and_where('end_date', '>=', $date)
            ->and_where(DB::expr(" and page_link like '%{$page_link}%' "))
            ->execute()->as_array();

        $arr = [];
        foreach ($promo_list as $k => $v) {
            $info    = Model_Promo::promo_detail($v['id']);
            $page_link = [];
            foreach ($info['page_link'] as $link) {
                $page_link[] = [
                    'name'    => $link['name'],
                    'type_id' => $link['type_id'],
                    'path'    => $link['path'],
                ];
            }
            $arr[$k] = [
                'id'         => $v['id'],
                'title'      => $info['title'],
                'start_date' => $info['start_date'],
                'end_date'   => $info['end_date'],
                'auto_time'  => $info['auto_time'],
                'type'       => $info['type'],
                'frequency'  => $info['frequency'],
                'page_link'  => $page_link,
            ];

            if($info['type']==1 || $info['type']==2){
                $arr[$k]['piclist'] = self::img_handle($info['img_url'], $info);
            }
            if($info['type']==3){
                $arr[$k]['coupon_list'] = self::coupon_list($info['coupon_id']);
                if($v['coupon_style']!=1){
                    $arr[$k]['coupon_img'] = $this->img_url('/uploads/background_img/marketing-background.png',600,956);
                }else{
                    $arr[$k]['coupon_img'] = $this->img_url($v['coupon_img'],600,956);
                }
            }
            if($info['type']==4){
                $video = explode('|',$info['video']);
                $arr[$k]['video'] = $this->img_url($video[0]);
            }
        }

        return $arr;
    }

    use Trait_Api_Promo;
}